# -*- coding: utf-8 -*-
import pytest
import logging

from selenium import webdriver
from selenium.webdriver.common.by import By

from pages.downloads_page import DownloadsPage
from pages.search_page import SearchPage
from pages.search_page import SearchResultsPage
from pages.login_page import LoginPage
from pages.signup_page import SignupPage
from pages.contact_page import ContactPage

from tests.test_parameters import ParametersSearch
from tests.test_parameters import ParametersLogin
from tests.test_parameters import ParametersSignup
from tests.test_parameters import ParametersContact


logging.basicConfig(format=u'%(filename)s[LINE:%(lineno)d]# %(levelname)-8s [%(asctime)s]  %(message)s',
                    level=logging.INFO, filename='tests.log')


@pytest.fixture(scope='session')
def driver(request):
    _driver = webdriver.Firefox()

    def close_driver():
        _driver.quit()

    request.addfinalizer(close_driver)
    return _driver

@pytest.fixture(scope='module',
                params=[(By.XPATH, '//*[@id="content_wrapper"]/div[1]/div/div[1]/div/div[2]/div[1]/div[1]/div/div/a'),
                        (By.XPATH, '//*[@id="content_wrapper"]/div[1]/div/div[1]/div/div[2]/div[2]/div[1]/div/div/a')])
def product(request):
    return request.param


def test_search(driver):
    search_page = SearchPage(driver)
    search_page.open()
    search_page.search(ParametersSearch.SEARCH_VDR)
    search_results_page = SearchResultsPage(driver)
    assert search_results_page.results_count() == ParametersSearch.SEARCH_VDR['results_count']
    search_results_page.click_element(ParametersSearch.SEARCH_VDR['acticle_number'])
    title = search_results_page.article_title()
    assert title == ParametersSearch.SEARCH_VDR['article_title']

def test_login(driver):
    login_page = LoginPage(driver)
    login_page.open()
    login_page.login(ParametersLogin.USER_LOGIN)

def test_signup(driver):
    signup_page = SignupPage(driver)
    signup_page.open()
    signup_page.signup(ParametersSignup.USER_SIGNUP)

def test_downloads_read_more(driver):
    downloads_page = DownloadsPage(driver)
    downloads_page.open()
    downloads_page.read_more()

def test_downloads_product(driver, product):
    downloads_page = DownloadsPage(driver)
    downloads_page.open()
    downloads_page.download_product(product)

def test_contact(driver):
    contact_page = ContactPage(driver)
    contact_page.open()
    contact_page.contact(ParametersContact.CONTACT_US)
